<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" /><meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>nanobench &mdash; nanobench  documentation</title>
      <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
      <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
    <link rel="shortcut icon" href="_static/favicon.ico"/>
  <!--[if lt IE 9]>
    <script src="_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script src="_static/jquery.js"></script>
        <script src="_static/_sphinx_javascript_frameworks_compat.js"></script>
        <script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
        <script src="_static/doctools.js"></script>
        <script src="_static/sphinx_highlight.js"></script>
    <script src="_static/js/theme.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Installation" href="tutorial.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="#" class="icon icon-home">
            nanobench
              <img src="_static/nanobench-logo.svg" class="logo" alt="Logo"/>
          </a>
              <div class="version">
                v4.3.11
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption" role="heading"><span class="caption-text">Getting Started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="tutorial.html">Installation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="tutorial.html#direct-inclusion">Direct Inclusion</a></li>
<li class="toctree-l2"><a class="reference internal" href="tutorial.html#cmake-integration">CMake Integration</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="tutorial.html#usage">Usage</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial.html#examples">Examples</a><ul>
<li class="toctree-l2"><a class="reference internal" href="tutorial.html#something-fast">Something Fast</a></li>
<li class="toctree-l2"><a class="reference internal" href="tutorial.html#something-slow">Something Slow</a></li>
<li class="toctree-l2"><a class="reference internal" href="tutorial.html#something-unstable">Something Unstable</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="tutorial.html#comparing-results">Comparing Results</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial.html#asymptotic-complexity">Asymptotic Complexity</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial.html#rendering-mustache-like-templates">Rendering Mustache-like Templates</a><ul>
<li class="toctree-l2"><a class="reference internal" href="tutorial.html#csv-comma-separated-values">CSV - Comma-Separated Values</a></li>
<li class="toctree-l2"><a class="reference internal" href="tutorial.html#html-box-plots">HTML Box Plots</a></li>
<li class="toctree-l2"><a class="reference internal" href="tutorial.html#json-javascript-object-notation">JSON - JavaScript Object Notation</a></li>
<li class="toctree-l2"><a class="reference internal" href="tutorial.html#pyperf-python-pyperf-module-output">pyperf - Python pyperf module Output</a><ul>
<li class="toctree-l3"><a class="reference internal" href="tutorial.html#show-benchmark-statistics">Show Benchmark Statistics</a></li>
<li class="toctree-l3"><a class="reference internal" href="tutorial.html#show-a-histogram">Show a Histogram</a></li>
<li class="toctree-l3"><a class="reference internal" href="tutorial.html#compare-results">Compare Results</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Comparison</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="comparison.html">Test Set</a></li>
<li class="toctree-l1"><a class="reference internal" href="comparison.html#runtime">Runtime</a></li>
<li class="toctree-l1"><a class="reference internal" href="comparison.html#implementations-output">Implementations &amp; Output</a><ul>
<li class="toctree-l2"><a class="reference internal" href="comparison.html#nanobench">nanobench</a><ul>
<li class="toctree-l3"><a class="reference internal" href="comparison.html#sourcecode">Sourcecode</a></li>
<li class="toctree-l3"><a class="reference internal" href="comparison.html#results">Results</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="comparison.html#google-benchmark">Google Benchmark</a><ul>
<li class="toctree-l3"><a class="reference internal" href="comparison.html#id2">Sourcecode</a></li>
<li class="toctree-l3"><a class="reference internal" href="comparison.html#id3">Results</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="comparison.html#nonius">nonius</a><ul>
<li class="toctree-l3"><a class="reference internal" href="comparison.html#id5">Sourcecode</a></li>
<li class="toctree-l3"><a class="reference internal" href="comparison.html#id6">Results</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="comparison.html#picobench">Picobench</a><ul>
<li class="toctree-l3"><a class="reference internal" href="comparison.html#id8">Sourcecode</a></li>
<li class="toctree-l3"><a class="reference internal" href="comparison.html#id9">Results</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="comparison.html#catch2">Catch2</a><ul>
<li class="toctree-l3"><a class="reference internal" href="comparison.html#id11">Sourcecode</a></li>
<li class="toctree-l3"><a class="reference internal" href="comparison.html#id12">Results</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="comparison.html#moodycamel-microbench">moodycamel::microbench</a><ul>
<li class="toctree-l3"><a class="reference internal" href="comparison.html#id14">Sourcecode</a></li>
<li class="toctree-l3"><a class="reference internal" href="comparison.html#id15">Results</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="comparison.html#sltbench">sltbench</a><ul>
<li class="toctree-l3"><a class="reference internal" href="comparison.html#id17">Sourcecode</a></li>
<li class="toctree-l3"><a class="reference internal" href="comparison.html#id18">Results</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="comparison.html#celero">Celero</a></li>
<li class="toctree-l2"><a class="reference internal" href="comparison.html#folly-benchmark">folly Benchmark</a></li>
</ul>
</li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Reference</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="reference.html"><code class="docutils literal notranslate"><span class="pre">ankerl::nanobench</span></code> Reference</a><ul>
<li class="toctree-l2"><a class="reference internal" href="reference.html#bench-main-entry-point"><code class="xref cpp cpp-class docutils literal notranslate"><span class="pre">Bench</span></code> - Main Entry Point</a></li>
<li class="toctree-l2"><a class="reference internal" href="reference.html#rng-extremely-fast-prng"><code class="xref cpp cpp-class docutils literal notranslate"><span class="pre">Rng</span></code> - Extremely fast PRNG</a></li>
<li class="toctree-l2"><a class="reference internal" href="reference.html#result-benchmark-results"><code class="xref cpp cpp-class docutils literal notranslate"><span class="pre">Result</span></code> - Benchmark Results</a></li>
<li class="toctree-l2"><a class="reference internal" href="reference.html#donotoptimizeaway"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">doNotOptimizeAway()</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="reference.html#render-mustache-like-templates"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">render()</span></code> - Mustache-like Templates</a><ul>
<li class="toctree-l3"><a class="reference internal" href="reference.html#templates-csv"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">templates::csv</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="reference.html#templates-htmlboxplot"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">templates::htmlBoxplot</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="reference.html#templates-json"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">templates::json</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="reference.html#templates-pyperf"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">templates::pyperf</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="reference.html#environment-variables">Environment Variables</a><ul>
<li class="toctree-l3"><a class="reference internal" href="reference.html#nanobench-endless-run-a-specific-test-endlessly"><code class="docutils literal notranslate"><span class="pre">NANOBENCH_ENDLESS</span></code> - Run a Specific Test Endlessly</a></li>
<li class="toctree-l3"><a class="reference internal" href="reference.html#nanobench-suppress-warnings-no-stability-warnings"><code class="docutils literal notranslate"><span class="pre">NANOBENCH_SUPPRESS_WARNINGS</span></code> - No Stability Warnings</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="genindex.html">Index</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">About</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="license.html">MIT License</a></li>
<li class="toctree-l1"><a class="reference internal" href="CODE_OF_CONDUCT.html">Contributor Covenant Code of Conduct</a><ul>
<li class="toctree-l2"><a class="reference internal" href="CODE_OF_CONDUCT.html#our-pledge">Our Pledge</a></li>
<li class="toctree-l2"><a class="reference internal" href="CODE_OF_CONDUCT.html#our-standards">Our Standards</a></li>
<li class="toctree-l2"><a class="reference internal" href="CODE_OF_CONDUCT.html#enforcement-responsibilities">Enforcement Responsibilities</a></li>
<li class="toctree-l2"><a class="reference internal" href="CODE_OF_CONDUCT.html#scope">Scope</a></li>
<li class="toctree-l2"><a class="reference internal" href="CODE_OF_CONDUCT.html#enforcement">Enforcement</a></li>
<li class="toctree-l2"><a class="reference internal" href="CODE_OF_CONDUCT.html#enforcement-guidelines">Enforcement Guidelines</a><ul>
<li class="toctree-l3"><a class="reference internal" href="CODE_OF_CONDUCT.html#correction">1. Correction</a></li>
<li class="toctree-l3"><a class="reference internal" href="CODE_OF_CONDUCT.html#warning">2. Warning</a></li>
<li class="toctree-l3"><a class="reference internal" href="CODE_OF_CONDUCT.html#temporary-ban">3. Temporary Ban</a></li>
<li class="toctree-l3"><a class="reference internal" href="CODE_OF_CONDUCT.html#permanent-ban">4. Permanent Ban</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="CODE_OF_CONDUCT.html#attribution">Attribution</a></li>
</ul>
</li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="#">nanobench</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="#" class="icon icon-home" aria-label="Home"></a></li>
      <li class="breadcrumb-item active">nanobench</li>
      <li class="wy-breadcrumbs-aside">
              <a href="https://github.com/martinus/nanobench/blob/master/src/docs/index.rst" class="fa fa-github"> Edit on GitHub</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="nanobench">
<h1>nanobench<a class="headerlink" href="#nanobench" title="Permalink to this heading"></a></h1>
<div class="toctree-wrapper compound">
</div>
<div class="toctree-wrapper compound">
</div>
<div class="toctree-wrapper compound">
</div>
<div class="toctree-wrapper compound">
</div>
<p><a class="reference external" href="https://github.com/martinus/nanobench/releases"><img alt="Release" src="https://img.shields.io/github/release/martinus/nanobench.svg" /></a> <a class="reference external" href="https://raw.githubusercontent.com/martinus/nanobench/master/LICENSE"><img alt="License" src="https://img.shields.io/github/license/martinus/nanobench.svg" /></a> <a class="reference external" href="https://travis-ci.com/martinus/nanobench"><img alt="Travis CI Build Status" src="https://travis-ci.com/martinus/nanobench.svg?branch=master" /></a> <a class="reference external" href="https://ci.appveyor.com/project/martinus/nanobench"><img alt="Appveyor Build Status" src="https://ci.appveyor.com/api/projects/status/github/martinus/nanobench?branch=master&amp;svg=true" /></a> <a class="reference external" href="https://gitter.im/nanobench/community?utm_source=badge&amp;utm_medium=badge&amp;utm_campaign=pr-badge&amp;utm_content=badge"><img alt="Join the chat at https://gitter.im/nanobench/community" src="https://badges.gitter.im/nanobench/community.svg" /></a></p>
<img alt="I need a better logo. Currently I use a small bench. Nanobench. Ha ha." src="_images/nanobench-logo-small.svg" /><div class="admonition hint">
<p class="admonition-title">Hint</p>
<p>View and download <a class="reference external" href="https://github.com/martinus/nanobench">nanobench on Github</a>.
Get the latest <a class="reference download external" download="" href="https://github.com/martinus/nanobench/releases/latest"><code class="xref download docutils literal notranslate"><span class="pre">release</span></code></a>.</p>
</div>
<p><code class="docutils literal notranslate"><span class="pre">ankerl::nanobench</span></code> is a platform independent microbenchmarking library for C++11/14/17/20.</p>
<div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="cp">#define ANKERL_NANOBENCH_IMPLEMENT</span>
<span class="linenos"> 2</span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;nanobench.h&gt;</span>
<span class="linenos"> 3</span>
<span class="linenos"> 4</span><span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 5</span><span class="w">    </span><span class="kt">double</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1.0</span><span class="p">;</span>
<span class="linenos"> 6</span><span class="w">    </span><span class="n">ankerl</span><span class="o">::</span><span class="n">nanobench</span><span class="o">::</span><span class="n">Bench</span><span class="p">().</span><span class="n">run</span><span class="p">(</span><span class="s">&quot;some double ops&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="p">]</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 7</span><span class="w">        </span><span class="n">d</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mf">1.0</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="n">d</span><span class="p">;</span>
<span class="linenos"> 8</span><span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">d</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mf">5.0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 9</span><span class="w">            </span><span class="n">d</span><span class="w"> </span><span class="o">-=</span><span class="w"> </span><span class="mf">5.0</span><span class="p">;</span>
<span class="linenos">10</span><span class="w">        </span><span class="p">}</span>
<span class="linenos">11</span><span class="w">        </span><span class="n">ankerl</span><span class="o">::</span><span class="n">nanobench</span><span class="o">::</span><span class="n">doNotOptimizeAway</span><span class="p">(</span><span class="n">d</span><span class="p">);</span>
<span class="linenos">12</span><span class="w">    </span><span class="p">});</span>
<span class="linenos">13</span><span class="p">}</span>
</pre></div>
</div>
<p>The whole executable runs for ~60ms and prints</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>|               ns/op |                op/s |    err% |          ins/op |          cyc/op |    IPC |         bra/op |   miss% |     total | benchmark
|--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:----------
|                7.52 |      132,948,239.79 |    1.1% |            6.65 |           24.07 |  0.276 |           1.00 |    8.9% |      0.00 | `some double ops`
</pre></div>
</div>
<p>Which github renders like</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>ns/op</p></th>
<th class="head"><p>op/s</p></th>
<th class="head"><p>err%</p></th>
<th class="head"><p>ins/op</p></th>
<th class="head"><p>cyc/op</p></th>
<th class="head"><p>IPC</p></th>
<th class="head"><p>bra/op</p></th>
<th class="head"><p>miss%</p></th>
<th class="head"><p>total</p></th>
<th class="head"><p>benchmark</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>7.52</p></td>
<td><p>132,948,239.79</p></td>
<td><p>1.1%</p></td>
<td><p>6.65</p></td>
<td><p>24.07</p></td>
<td><p>0.276</p></td>
<td><p>1.00</p></td>
<td><p>8.9%</p></td>
<td><p>0.00</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">some</span> <span class="pre">double</span> <span class="pre">ops</span></code></p></td>
</tr>
</tbody>
</table>
<p>The benchmarked code takes <strong>7.52</strong> nanoseconds to run, so <strong>~133</strong> million times per seconds. Measurements fluctuate by
<strong>1.1%</strong>. On average <strong>6.65</strong> instructions are executed in <strong>24.07</strong> CPU cycles, resulting in <strong>0.276</strong> instructions per
cycle. A <strong>single</strong> branch is in the code, which branch prediction missed in <strong>8.9%</strong> of the cases. Total runtime of
the benchmark with the name <code class="docutils literal notranslate"><span class="pre">some</span> <span class="pre">double</span> <span class="pre">ops</span></code> is <strong>0.00</strong>, so just a few milliseconds.</p>
<section id="design-goals">
<h2>Design Goals<a class="headerlink" href="#design-goals" title="Permalink to this heading"></a></h2>
<dl class="simple">
<dt>Ease of use</dt><dd><p>Simple but powerful API, fast compile times, easy to integrate anywhere.</p>
</dd>
<dt>Fast</dt><dd><p>Get accurate results as fast as possible</p>
</dd>
<dt>Accurate</dt><dd><p>Get deterministic, repeatable, and accurate results that you can make sound decisions on.</p>
</dd>
<dt>Robust</dt><dd><p>Be robust against outliers, warn if results are not reliable.</p>
</dd>
</dl>
</section>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="tutorial.html" class="btn btn-neutral float-right" title="Installation" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2019-2023 Martin Leitner-Ankerl &lt;martin.ankerl@gmail.com&gt;.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>
    <!-- Theme Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-36863101-2"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'UA-36863101-2', {
          'anonymize_ip': false,
      });
    </script> 

</body>
</html>